/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.weather;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class WeatherLocXmlHandler extends DefaultHandler
{
    private static String LOCATION_TAG = "loc";
    private static String ID_ATTR = "id";

    private String _locId = null;
    private String _locationName = null;
    private StringBuffer _characters = new StringBuffer();
    private boolean _inLoc = false;

    public WeatherLocXmlHandler ()
    {
        //nothing
    }

    public boolean hasLocationId () 
    {
        return ( _locId != null && _locId.length() > 0 );
    }

    public String getLocationId () { return _locId; }
    public String getLocationName () { return _locationName; }

    public void startElement ( String uri, String localName,
                               String qName, Attributes attributes )
    {
        if ( qName.equalsIgnoreCase( LOCATION_TAG ) )
        {
            clearCharacters();
            _inLoc = true;
            _locId = attributes.getValue( ID_ATTR );
        }
    }

    public void endElement ( String uri, String localName, String qName )
    {
        if ( qName.equalsIgnoreCase( LOCATION_TAG ) )
        {
            _locationName = getCharacters();
            _inLoc = false;
        }
    }

    public void characters ( char [] ch, int start, int length )
    {
        _characters.append( ch, start, length );
    }

    private String getCharacters()
    {
        return _characters.toString();
    }

    private void clearCharacters ()
    {
        _characters = new StringBuffer();
    }

}